package com.lz.sentinel.project.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lz.sentinel.common.entity.RequestVo;
import com.lz.sentinel.common.entity.ResponseVo;
import com.lz.sentinel.common.utils.DateUtils;
import org.springframework.web.bind.annotation.*;

/**
 * MQ生产者
 *
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/instrument")
public class InstrumentController {

    @PostMapping("/query")
    @SentinelResource(value = "instrument", blockHandler = "handleBlock", fallback = "handleFallback")
    public ResponseVo send(@RequestBody RequestVo requestVo) {
        System.out.println(DateUtils.getCurr_YYYY_MM_DD_HH_MM_SS_SSS() + "： instrument query");
        try {
            Thread.sleep(RandomUtil.randomLong(1, 10) * 300);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return ResponseVo.success(" instrument query 成功");
    }

    // 降级处理方法
    public String handleFallback(@RequestBody RequestVo requestVo) {
        return "Degrade response";
    }

    public ResponseVo handleBlock(@RequestBody RequestVo requestVo, BlockException e) {
        ResponseVo result = new ResponseVo();
        result.setErrorCode(-1);
        result.setErrorMsg(" 熔断控制了！");
        return result;
    }

}